package com.zsc.projectmanagementapi.service.Impl;

import com.zsc.projectmanagementapi.dao.ProjectMapper;
import com.zsc.projectmanagementapi.dao.ProjectPlanMapper;
import com.zsc.projectmanagementapi.dao.ProjectStateMapper;
import com.zsc.projectmanagementapi.pojo.Dao.CreateProjectDao;
import com.zsc.projectmanagementapi.pojo.Do.Project;
import com.zsc.projectmanagementapi.pojo.Do.ProjectPlan;
import com.zsc.projectmanagementapi.pojo.Do.ProjectState;
import com.zsc.projectmanagementapi.service.ProjectService;
import com.zsc.projectmanagementapi.service.ProjectStateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author LinHj
 * @date 2021/6/30 16:04
 **/
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    ProjectPlanMapper projectPlanMapper;
    @Autowired
    ProjectStateMapper projectStateMapper;

    @Override
    @Transactional
    public int insertProjectAndPlan(CreateProjectDao createProjectDao) {
        Project project = new Project();
        ProjectPlan projectPlan = new ProjectPlan();
        BeanUtils.copyProperties(createProjectDao,project);
        project.setCreatedTime(new Date());
        projectMapper.insert(project);

        ProjectState projectState = new ProjectState();
        projectState.setProjectId(project.getId());
        projectState.setProjectName(project.getName());
        projectState.setBeginTime(createProjectDao.getStartDate());
        projectState.setDeadline(createProjectDao.getEndDate());
        projectState.setImportance(1);
        projectState.setState(0);
        projectStateMapper.insert(projectState);

        BeanUtils.copyProperties(createProjectDao,projectPlan);
        System.out.println(project.getId());
        projectPlan.setProjectId(project.getId());
        projectPlanMapper.insert(projectPlan);

        return 1;
    }
}
